package com.example.springboot.ytest.ytest;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Map 转 Object
 * 
 * @author: yeyi
 * @date: 2019年6月6日
 */
public class MapToObject {
	
	/**
	 * 如果是内部类一定要用静态类，外部可以不静态
	 * 
	 * @author: yeyi
	 * @date: 2019年6月6日
	 */
	static class TestBean {
		private String s;
		private Integer i;
		
		public String getS() {
			return s;
		}
		public void setS(String s) {
			this.s = s;
		}
		public Integer getI() {
			return i;
		}
		public void setI(Integer i) {
			this.i = i;
		}
	}
	
	static void map2Obj() {
		Map<String, Object> m1 = new HashMap<>();
		m1.put("s", "字符");
		m1.put("i", 4);
		final ObjectMapper mapper = new ObjectMapper();
		// 转单个对象
		TestBean tb = mapper.convertValue(m1, TestBean.class); 
		System.out.println("+++++++: " + JSON.toJSONString(tb, true));
		
		List<Map<String, Object>> mapList = new LinkedList<>();
		mapList.add(m1);
		// 转列表
		List<TestBean> tmp = mapper.convertValue(mapList, new TypeReference<List<TestBean>>(){});
		System.out.println("-------: " + JSON.toJSONString(tmp, true));
	}
	
	public static void main(String[] args) {
		map2Obj();
	}
}
